// JavaScript Document
//@require Class.js

Voltimetro = new MyClass({
	/**  
     * Property: voltA
	 * 	 Voltaje del voltimetro A
     * {Integer} values: 0 to 200
     */
	voltA: 0, 
	/**  
     * Property: voltB
	 *	Voltaje del voltimetro B
     * {Integer} values: 0 to 200
     */
	voltB: 0,
	/**  
     * Property: imagesURL
     * {String}
     */
	imagesURL: 'js/tablero/images/', 
	/** 
     * Property: display
     * {DOMElement} 
     */
	display: null, 
	/** 
     * SIZE of DISPLAY
     */
	width: '120px',
	height: '100px',
	/** 
     * Property: agujaVoltA
     * {DOMElement: div} 
     */
	agujaVoltA: null, 
	agujaVoltAWidth: '1200px',
	/** 
     * Property: agujaVoltB
     * {DOMElement: div} 
     */
	agujaVoltB: null, 
	agujaVoltBWidth: '1200px',
	/** 
     * Property: maxima
     * {Integer} 
     */
	voltAMaxima: 30,
	/** 
     * Property: maxima
     * {Integer} 
     */
	voltBMaxima: 30,
	/** 
     * Property: animationFuelTimeOut
     * {Integer} 
     */
	animationVoltATimeOut: null,
	/** 
     * Property: animationFuelTimeOut
     * {Integer} 
     */
	animationVoltBTimeOut: null,
	/** 
     * Property: activeTemp
     * {boolean} 
     */
	activeVoltA: true,
	/** 
     * Property: activeFuel
     * {boolean} 
     */
	activeVoltB: true,

	init: function(imagesURL){
		if (imagesURL!=null)
			this.imagesURL = imagesURL;
		this.display = document.createElement("div");
		with (this.display.style){
			width = this.width;
			height = this.height;
			background = "transparent url("+this.imagesURL+"voltimetro.gif) no-repeat center";
		}
		
		this.agujaVoltA = document.createElement("div");
		with (this.agujaVoltA.style){
			width = this.width;
			height = this.height;
			background = "transparent url("+this.imagesURL+"agujaVoltimetroA.gif) no-repeat";			
		}
		this.display.appendChild(this.agujaVoltA);		
		
		this.agujaVoltB = document.createElement("div");
		with (this.agujaVoltB.style){
			width = this.width;
			height = this.height;
			background = "transparent url("+this.imagesURL+"agujaVoltimetroB.gif) no-repeat";			
		}
		this.agujaVoltA.appendChild(this.agujaVoltB);						
	},
	
	/** 
     * Method: putDisplayIn
     *  
     * Parameters:
     * idElement - {String} 
     */
	putDisplayIn: function(idElement){
		document.getElementById(idElement).appendChild(this.display);
	},
	
	/** 
     * Method: activateVoltA
     *  Activa/desactiva el voltimetro A
     * Parameters:
     * 	[state] - {Boolean} 
     */
	activateVoltA: function(state) {
		if ((state!=null)&&(state==false)){
			if (this.activeVoltA){
				this.activeVoltA = false;
				this.agujaVoltA.style.background = "";
				this.voltA = 0;
			}
		}
		else{
			if (!this.activeVoltA){
				this.activeVoltA = true;
				this.agujaVoltA.style.background = "transparent url("+this.imagesURL+"agujaVoltimetroA.gif) no-repeat";
			}
		}		
	},
	
	/** 
     * Method: setVoltA
     *  Setea un voltaje, si el valor del parametro es 'null' desactiva el voltimetro
     * Parameters:
     * 	value - {Integer} 
     */
	setVoltA: function(value) {
		//activate/desctivate----------------------
		if ((value==null)||(isNaN(value)))
			this.activateVoltA(false);
		else	
			this.activateVoltA(true);
		if (!this.activeVoltA) return;
		//------------------------------------------
		value = parseFloat(value);
		value = Math.floor(value);
		if (value > this.voltAMaxima)
			value = this.voltAMaxima;
		else
			if (value < 0)
				value = 0;
		var agujaFrames = Math.floor(parseInt(this.agujaVoltAWidth)/parseInt(this.width))-1;
		var val=0;
		if (agujaFrames>1){
			var interval = this.voltAMaxima/agujaFrames;
			var val = Math.round(value/interval);
		}
		var offsetX = (-1) * val * parseInt(this.width);
		var backgroundPosition = offsetX+'px 0px';		
		this.agujaVoltA.style.backgroundPosition = backgroundPosition;
		this.voltA = value;
		this.generateTooltip();
	},	
	
	/** 
     * Method: moveToVoltA
     *  
     * Parameters:
     * 	toValue - {Integer} 
     */
	moveToVoltA: function(toValue) {
		//activate/desctivate----------------------
		if ((toValue==null)||(isNaN(toValue)))
			this.activateVoltA(false);
		else	
			this.activateVoltA(true);
		if (!this.activeVoltA){
			this.generateTooltip();
			return;
			}
		//------------------------------------------
		if(this.animationVoltATimeOut!=null)
			clearTimeout(this.animationVoltATimeOut);
		toValue = parseFloat(toValue);
		toValue = Math.floor(toValue);
		if (toValue > this.voltAMaxima)
			toValue = this.voltAMaxima;
		else
			if (toValue < 0)
				toValue = 0;
		var fromValue = this.voltA;
		var inc = true;
		if (fromValue>toValue)
			inc = false;
		var value = fromValue;
		var me = this;
		var f=function animated(){	
			(inc)?value++:value--;
			me.setVoltA(value);				
			if (value!=toValue){
				me.animationVoltATimeOut=setTimeout(f,10);
			}
		}
		if (toValue!=value)
			this.animationVoltATimeOut=setTimeout(f,10);
	},
	
	/** 
     * Method: activateVoltB
     *  Activa/desactiva el voltimetro A
     * Parameters:
     * 	[state] - {Boolean} 
     */
	activateVoltB: function(state) {
		if ((state!=null)&&(state==false)){
			if (this.activeVoltB){
				this.activeVoltB = false;
				this.agujaVoltB.style.background = "";
				this.voltB = 0;
			}
		}
		else{
			if (!this.activeVoltB){
				this.activeVoltB = true;
				this.agujaVoltB.style.background = "transparent url("+this.imagesURL+"agujaVoltimetroB.gif) no-repeat";
			}
		}		
	},
	
	/** 
     * Method: setVoltB
     *  Setea un voltaje, si el valor del parametro es 'null' desactiva el voltimetro
     * Parameters:
     * 	value - {Integer} 
     */
	setVoltB: function(value) {
		//activate/desctivate----------------------
		if ((value==null)||(isNaN(value)))
			this.activateVoltB(false);
		else	
			this.activateVoltB(true);
		if (!this.activeVoltB) return;
		//------------------------------------------
		value = parseFloat(value);
		value = Math.floor(value);
		if (value > this.voltBMaxima)
			value = this.voltBMaxima;
		else
			if (value < 0)
				value = 0;
		var agujaFrames = Math.floor(parseInt(this.agujaVoltBWidth)/parseInt(this.width))-1;
		var val=0;
		if (agujaFrames>1){
			var interval = this.voltBMaxima/agujaFrames;
			var val = Math.round(value/interval);
		}
		var offsetX = (-1) * val * parseInt(this.width);
		var backgroundPosition = offsetX+'px 0px';		
		this.agujaVoltB.style.backgroundPosition = backgroundPosition;
		this.voltB = value;
		this.generateTooltip();
	},	
	
	/** 
     * Method: moveToVoltB
     *  
     * Parameters:
     * 	toValue - {Integer} 
     */
	moveToVoltB: function(toValue) {
		//activate/desctivate----------------------
		if ((toValue==null)||(isNaN(toValue)))
			this.activateVoltB(false);			
		else	
			this.activateVoltB(true);
		if (!this.activeVoltB){
			this.generateTooltip();
			return;
			}
		//------------------------------------------
		if(this.animationVoltBTimeOut!=null)
			clearTimeout(this.animationVoltBTimeOut);
		toValue = parseFloat(toValue);
		toValue = Math.floor(toValue);
		if (toValue > this.voltBMaxima)
			toValue = this.voltBMaxima;
		else
			if (toValue < 0)
				toValue = 0;
		var fromValue = this.voltB;
		var inc = true;
		if (fromValue>toValue)
			inc = false;
		var value = fromValue;
		var me = this;
		var f=function animated(){	
			(inc)?value++:value--;
			me.setVoltB(value);				
			if (value!=toValue){
				me.animationVoltBTimeOut=setTimeout(f,10);
			}
		}
		if (toValue!=value)
			this.animationVoltBTimeOut=setTimeout(f,10);
	},
	
	/** 
     * Method: generateTooltip
	 *   Formatea e inserta el tooltip (title) al componente actual
     * Parameters:
     * 	toValue - {Integer} 
     */
	generateTooltip: function(){
		var texto = 'VOLTIMETRO: \n ';
		texto += 'Bateria principal (A) = ';
		if (this.activeVoltA)
			texto += this.voltA + ' volts.'
		else texto += 'desactivado';
		texto += ' \n ';
		texto += 'Bateria secundaria (B) = ';
		if (this.activeVoltB)
			texto += this.voltB + ' volts.'
		else texto += 'desactivado';
		this.display.title = texto;
	}
	
});